package com.aierkeji.call.b2c.service;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aierkeji.call.b2c.contants.Contants;
import com.aierkeji.call.b2c.dao.jpa.AkjCallApprovalHistroDao;
import com.aierkeji.call.b2c.dao.jpa.AkjCallRoleDao;
import com.aierkeji.call.b2c.dao.jpa.AkjCallUserDao;
import com.aierkeji.call.b2c.entity.AkjCallApprovalHistro;
import com.aierkeji.call.b2c.entity.AkjCallApprovalHistroVo;
import com.aierkeji.call.b2c.entity.AkjCallRole;
import com.aierkeji.call.b2c.entity.AkjCallUser;
import com.aierkeji.call.b2c.entity.PageBean;
import com.aierkeji.call.b2c.entity.contants.ApprovalResult;
import com.aierkeji.call.b2c.entity.snap.AkjCallUserSnap;
import com.aierkeji.call.b2c.util.DateTimeUtils;
import com.aierkeji.call.b2c.util.GsonUtils;
import com.aierkeji.call.b2c.util.ResultMapHelper;
import com.aierkeji.call.b2c.util.StringUtils;

@Service
public class AkjCallApprovalHistroService {
	
	
	private final Logger log = LogManager.getLogger(getClass());
	
	@Autowired
	private AkjCallApprovalHistroDao akjCallApprovalHistroDao;
	
	@Autowired
	private AkjCallUserDao akjCallUserDao;
	
	@Autowired
	private AkjCallRoleDao akjCallRoleDao;
	
	
	public Map<String, Object> findApply(Map<String, Object> map) {
		
	String page = (String)map.get("page");
	
	if(StringUtils.isEmpty(page)) {
		return ResultMapHelper.nullParamsResult();
	}
		
	AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
	
	PageBean<AkjCallApprovalHistroVo> approvalHistroVoPage = null;
	
	Long id = akjCallUser.getId();
	
	map.put("approvalId",id);
	
	approvalHistroVoPage = akjCallApprovalHistroDao.findApprovalByApprovalIdAndApprovalResultforPage(map); 
	
	return ResultMapHelper.success(approvalHistroVoPage);
}

	@Transactional(rollbackOn=Exception.class)
	public Map<String, Object> approvalApplyForUser(Map<String, Object> map) throws Exception{
		
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		
		try {
			List<Map<String, Object>> list 	= (List<Map<String, Object>>)map.get("source");
			List<AkjCallRole> roleList 		= akjCallRoleDao.findByCode(Contants.ROLE_ID_SALES);
			
			for(Map<String, Object> maps : list) {
				
				AkjCallApprovalHistroVo vo = GsonUtils.transMapToObj(maps, AkjCallApprovalHistroVo.class);
			
				if(StringUtils.checkAnyNull(vo.getPhone(),vo.getApprovalResult())) {
					return ResultMapHelper.nullParamsResult();
				}
				if(vo.getId() == null) {
					return ResultMapHelper.nullParamsResult();
				}
				
				AkjCallApprovalHistro approvalHistro =  akjCallApprovalHistroDao.findById(vo.getId());
				approvalHistro.setApprovalResult(ApprovalResult.valueOf(vo.getApprovalResult()));
				approvalHistro.setRemark(vo.getRemark());
				approvalHistro.setDelete(false);
				approvalHistro.setApprovalAt(DateTimeUtils.getCurrentTime());
				akjCallApprovalHistroDao.save(approvalHistro);
				if(vo.getApprovalResult().equals("Y")) {
					AkjCallUser applyUser = akjCallUserDao.findByAccountAndIsDelete(vo.getPhone(), false);
					applyUser.setLeaderId(akjCallUser.getId());
					applyUser.setEnterprise(akjCallUser.getEnterprise());
					applyUser.setRoles(roleList);
					applyUser.setApprovedAt(DateTimeUtils.getCurrentTime());
					applyUser.setUpdatedAt(DateTimeUtils.getCurrentTime());
					applyUser.setUpdatedBy(AkjCallUserSnap.build(akjCallUser));
					akjCallUserDao.save(applyUser);
				}
			}
		
		} catch (Exception e) {
			log.error("error rollback for AkjCallApprovalHistroService.approvalApplyForUser",e);
			throw new Exception(" error rollback for AkjCallApprovalHistroService.approvalApplyForUser ",e);
		}
		
		return ResultMapHelper.success();
	}

	public Map<String, Object> applyResult(Map<String, Object> map) {
		
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		
		List<AkjCallApprovalHistro> approvalHistro = akjCallApprovalHistroDao.findByApplyUserIdAndIsDelete(AkjCallUserSnap.build(akjCallUser),false);
		
		if(approvalHistro.size()<=0) {
			return ResultMapHelper.success();
		}
		
		return ResultMapHelper.success(approvalHistro);
	}

/*	public Map<String, Object> checkApplyResult(Map<String, Object> map) {
		
		String id = (String)map.get("id");
	
		if(StringUtils.isEmpty(id) ) {
			return ResultMapHelper.nullParamsResult();
		}
		AkjCallApprovalHistro approvalHistro = akjCallApprovalHistroDao.findById(Long.valueOf(id));
		approvalHistro.setApplyCheck(ApplyCheck.Y);
		akjCallApprovalHistroDao.save(approvalHistro);
		
		return ResultMapHelper.success();
	}*/

	public Map<String, Object> displayApprovalResult(Map<String, Object> map) {
		
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		String id = (String)map.get("id");
		
		
		if(StringUtils.isNotEmpty(id)) {
			AkjCallApprovalHistro approvalHistro = akjCallApprovalHistroDao.findById(Long.valueOf(id));
			approvalHistro.setDelete(true);
			akjCallApprovalHistroDao.save(approvalHistro);
			
			return ResultMapHelper.success(); 
		}
		
		
		List<AkjCallApprovalHistro> approvalHistro = akjCallApprovalHistroDao.findByApplyUserIdAndIsDelete(AkjCallUserSnap.build(akjCallUser),false);
		
		for(AkjCallApprovalHistro aH : approvalHistro) {
			aH.setDelete(true);
			akjCallApprovalHistroDao.save(aH);
		}
		
		return ResultMapHelper.success();
	}

}
